package com.kdd.campus.sh.controller;

import com.kdd.campus.common.entity.CommonResult;
import com.kdd.campus.common.util.RedisUtil;
import com.kdd.campus.sh.dto.CartDto;
import com.kdd.campus.sh.po.ShCart;
import com.kdd.campus.sh.service.CartService;
import com.kdd.campus.sh.util.JwtTokenUtil;
import io.swagger.annotations.*;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "购物车")
@RestController
@RequestMapping("/cart")
@CrossOrigin
@Validated
public class CartController extends BaseController{

    @Autowired
    private CartService cartService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;


    @ApiOperation(value = "根据ID删除购物车某条数据")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Long")
    @DeleteMapping(value = "/{id}" )
    public CommonResult delete(@PathVariable @NotNull Long id){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));

        //调用ShCartService实现根据主键删除
        cartService.delete(userId,id);
        return CommonResult.success();
    }

    @ApiOperation(value = "清空购物车")
    @DeleteMapping(value = "/empty" )
    public CommonResult empty(){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));

        //调用ShCartService实现根据主键删除
        cartService.empty(userId);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改购物车商品的数量信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "cartId", value = "购物车id", dataType = "Long"),
            @ApiImplicitParam(paramType = "query",name = "productNum", value = "修改后的商品数量", dataType = "Integer"),
    })
    @PutMapping(value="/updateProductNum")
    public CommonResult update(@NotNull Long cartId, @Range(min = 1)@NotNull Integer productNum){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));

        cartService.update(userId,cartId,productNum);
        return CommonResult.success();
    }

    /***
     * 添加商品到购物车
     * @param cart
     * @return
     */
    @ApiOperation(value = "添加商品到购物车")
    @PostMapping
    public CommonResult add(@RequestBody  @ApiParam(name = "ShCart对象",value = "必须：商品id",required = true)@Validated ShCart cart){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        cart.setUserId(userId);
        //调用ShCartService实现添加ShCart
        cartService.add(cart);
        return CommonResult.success();
    }

    @ApiOperation(value = "查询用户购物车信息")
    @GetMapping
    public CommonResult findById(){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        List<CartDto> cartList = cartService.findByUserId(userId);
        return CommonResult.success(cartList);
    }
}
